package com.hbx.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.commonutils.R;
import com.hbx.eduservice.entity.EduCourse;
import com.hbx.eduservice.entity.dto.CourseInfoVo;
import com.hbx.eduservice.entity.dto.CoursePublishVo;
import com.hbx.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2023-10-02
 */
@RestController
@CrossOrigin
@Api(tags = {"课程管理"})
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //课程列表分页(条件分页查询)
    @PostMapping("/pageCourse/{current}/{limit}")
    public R getCourseListPage(@PathVariable Long current,@PathVariable Long limit,@RequestBody EduCourse courseQuery){
        Page<EduCourse> page = new Page<>(current,limit);

        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(courseQuery.getTitle()),EduCourse::getTitle,courseQuery.getTitle());
        queryWrapper.eq(!StringUtils.isEmpty(courseQuery.getStatus()),EduCourse::getStatus,courseQuery.getStatus());

        IPage<EduCourse> pageInfo = eduCourseService.page(page, queryWrapper);
        List<EduCourse> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后的课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //查询课程发布信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourseInfo",coursePublishVo);
    }

    //课程最终发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = eduCourseService.getById(id);
        course.setStatus("Normal");
        eduCourseService.updateById(course);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}

